package com.lantosec.hmrp.modules.system.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.mvc.BaseController;
import com.lantosec.hmrp.modules.system.entity.SysDept;
import com.lantosec.hmrp.modules.system.entity.SysDeptList;
import com.lantosec.hmrp.modules.system.entity.SysDeptPosition;
import com.lantosec.hmrp.modules.system.entity.SysDeptUser;
import com.lantosec.hmrp.modules.system.service.ISysDeptService;
import com.lantosec.hmrp.modules.system.vo.SysDeptVO;
import com.lantosec.hmrp.modules.system.vo.SysUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 *
 * 部门表 控制器
 *
 * @author AutoCode
 * @since 2020-02-12
 */
@Api(value = "部门表控制器", tags = "部门表控制器")
@RestController
@RequestMapping("/system/dept")
@Log4j2
public class SysDeptController extends BaseController {
    @Autowired
    private ISysDeptService service;

    @GetMapping("all")
    @ApiOperation(value = "查询全部")
    public Result<List<SysDeptVO>> all(){
        return Result.ok(entity2VO(service.list()));
    }

    @GetMapping("getDeptAll")
    @ApiOperation(value = "分页查询")
    public Result<PageVO<SysDeptVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<SysDeptVO> page){
        service.getSysDeptPage(page);
        return Result.ok(page);
    }

    @GetMapping("get/{id}")
    @ApiOperation(value = "主键查询")
    public Result<SysDeptVO> get(@ApiParam(value = "ID", required = true) @PathVariable(name="id") String id){
        return Result.ok(entity2VO(service.getById(id)));
    }

    @PostMapping("insertDeptInfo")
    @ApiOperation(value = "新增数据")
    public Result<String> insertDeptInfo(@ApiParam(value = "部门岗位组装对象", required = true) @RequestBody @Validated SysDeptVO sysDeptVO){
        service.insertDeptInfo(sysDeptVO);
        return Result.ok();
    }

    @PutMapping("updateDeptInfo")
    @ApiOperation(value = "修改数据")
    public Result<Void> updateDeptInfo(@ApiParam(value = "部门表VO对象", required = true) @RequestBody SysDeptVO sysDeptVO){
        service.updateDeptInfo(sysDeptVO);
        return Result.ok();
    }

    @DeleteMapping(value = "deleteDeptInfo/{ids}")
    @ApiOperation("删除部门")
    public Result<Void> deleteDeptInfo(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        Result result = service.deleteDeptInfo(ids);
        return result;
    }

    @DeleteMapping(value = "deleteDeptUser/{userId}")
    @ApiOperation("删除部门下的人员")
    public Result<Void> deleteDeptUser(@ApiParam(value = "人员ID", required = true) @PathVariable(name="userId") String userId){
        service.deleteDeptUser(userId);
        return Result.ok();
    }

    @PutMapping("updateOrder")
    @ApiOperation(value = "更新部门管理排序")
    public Result<Void> updateOrder(@ApiParam(value = "部门ID", required = true) @RequestParam(name = "id") String id,
                                    @ApiParam(value = "排序值", required = true) @RequestParam(name = "deptOrder") Integer deptOrder) {
        service.updateOrder(id,deptOrder);
        return Result.ok();
    }

    @PutMapping("updatePosition")
    @ApiOperation(value = "更新部门岗位")
    public Result<Void> updatePosition(@ApiParam(value = "部门ID", required = true) @RequestParam(name = "deptId") String deptId,
                                       @ApiParam(value = "职位列表", required = true) @RequestBody JSONArray positions){
        service.updatePosition(deptId,positions.toJSONString());
        return Result.ok();
    }

    @GetMapping("getDeptTree")
    @ApiOperation(value = "获得部门树")
    public Result<List<TreeNodeVo>> getDeptTree() {
        return Result.ok(service.deptTree());
    }

    @GetMapping("getDeptTreeByType")
    @ApiOperation(value = "根据部门类型获得部门树")
    public Result<List<TreeNodeVo>> getDeptTreeByType(@ApiParam(value = "部门ID") @RequestParam(name = "deptId",required = false) String deptId,
                                                      @ApiParam(value = "部门类型") @RequestParam(name = "deptType",required = false) String deptType) {
        return Result.ok(service.getDeptTreeByType(deptId,deptType));
    }

    @GetMapping("getDeptPositionTree")
    @ApiOperation(value = "获得部门岗位树")
    public Result<List<TreeNodeVo>> getDeptPositionTree() {
        return Result.ok(service.getDeptPositionTree());
    }

    @GetMapping("getDeptUserTree")
    @ApiOperation(value = "获得部门人员岗位树")
    public Result<List<TreeNodeVo>> getDeptUserTree() {
        return Result.ok(service.getDeptUserTree());
    }

    @GetMapping("getDeptTree/{id}")
    @ApiOperation(value = "根据指定ID查询对应的部门树")
    public Result<List<TreeNodeVo>> getDeptTree(@PathVariable(name = "id") String id) {
        return Result.ok(service.deptTree(id));
    }

    @GetMapping("getDeptById")
    @ApiOperation(value = "查询当前部门下的子部门")
    public Result<List<SysDeptVO>> getDeptById(@ApiParam(value = "ID") @RequestParam(name="id",required = false) String id,
                                               @ApiParam(value = "deptName") @RequestParam(name="deptName",required = false) String deptName){
        List<SysDeptVO> sysDeptList = service.getDeptPage(id,deptName);
        return Result.ok(sysDeptList);
    }

    @GetMapping("getDeptAllById")
    @ApiOperation(value = "查询当前部门")
    public Result<List<SysDeptVO>> getDeptAllById(@ApiParam(value = "ID") @RequestParam(name="id",required = false) String id){
        List<SysDeptVO> sysDeptVOList = service.getDeptAllById(id);
        return Result.ok(sysDeptVOList);
    }

    @PutMapping("updateManagerPost")
    @ApiOperation(value = "设置部门岗位为管理岗位")
    public Result<Void> updateManagerPost(@ApiParam(value = "部门岗位挂接表", required = true) @RequestBody SysDeptPosition sysDeptPosition){
        String deptId = sysDeptPosition.getDeptId();
        String positionId = sysDeptPosition.getPositionId();
        Integer state = sysDeptPosition.getState();
        service.updateManagerPost(deptId,positionId,state);
        return Result.ok();
    }

    @GetMapping("getDeptByUserId")
    @ApiOperation(value = "根据人员id查询部门(默认主部门)")
    public Result<SysDeptUser> getDeptByUserId(@ApiParam(value = "userId") @RequestParam(name="userId") String userId){
        SysDeptUser sysDeptUser = service.getDeptByUserId(userId);
        return Result.ok(sysDeptUser);
    }

    @PutMapping("addUserFromDept")
    @ApiOperation(value = "部门人员调动")
    public Result<Void> addUserFromDept(@ApiParam(value = "人员信息", required = true) @RequestBody SysUserVO sysUserVO){
        service.addUserFromDept(sysUserVO);
        return Result.ok();
    }

}
